//
//  BNTabItemView.swift
//  BNUQuoteTrade
//
//  Created by bebull on 2021/12/23.
//

import UIKit


class BNTabItemView: UICollectionViewCell {
    
    // MARK: - Properties
    // MARK: Public
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        return label
    }()
    var item: BNTabItem?
    var hideRedDot: Bool = true {
        didSet {
            redDotView.isHidden = hideRedDot
        }
    }
    // MARK: Privates
    /// 小红点
    private lazy var redDotView: UIView = {
        let view = UIView()
        view.backgroundColor = .red
        view.isHidden = true
        view.layer.cornerRadius = 4
        view.layer.masksToBounds = true
        return view
    }()
    
    
    
    // MARK: - Init
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(titleLabel)
        contentView.addSubview(redDotView)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Override
    override func layoutSubviews() {
        super.layoutSubviews()
        titleLabel.center = contentView.center
        redDotView.frame = CGRect(x: titleLabel.frame.origin.x + titleLabel.frame.size.width, y: titleLabel.frame.origin.y - 2.5, width: 8, height: 8)
    }
    
    // MARK: - Publics
    func reload(item: BNTabItem) {
        self.item = item
        if item.selected {
            titleLabel.font = item.titleSelectedFont
            titleLabel.textColor = item.titleSelectedColor
            backgroundColor = item.selectedColor
        } else {
            titleLabel.font = item.titleFont
            titleLabel.textColor = item.titleColor
            backgroundColor = item.color
        }
        
        titleLabel.text = item.title
        titleLabel.sizeToFit()
        
        setNeedsLayout()
        layoutIfNeeded()
    }
    
}

extension BNTabItemView {
    
    // MARK: - Notifications
    
    // MARK: - Action
    
    // MARK: - Privates
    
}
